package com.example.model;

import java.util.ArrayList;

import android.util.Log;

/**
 * This class will manage a user's account information, such as their password,
 * email, other contact information, a list of items they create, a list of
 * messages they sent or received, and maintain a log of said activities.
 * 
 * @author 3 Factorial
 */
public class User {

	private String name;
	private String user_name;
	private String password;
	private String address;
	private String phoneNumber;
	private String email;
	private int passwordCount;
	private boolean locked;
	private boolean admin;
	private ArrayList<Item> items;

	/**
	 * Creates a user with the user_name and password of choice.
	 * 
	 * @param user_name
	 *            User's desired user name.
	 * @param password
	 *            User's desired password.
	 */
	public User(String user_name, String password) {
		this.user_name = user_name;
		this.password = password;
		locked = false;
		admin = false;
		items = new ArrayList<Item>();
	} // End username, password constructor

	/*
	 * The following are getters/setters for all class variables so far.
	 */

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the user_name
	 */
	public String getUserName() {
		return user_name;
	}

	/**
	 * @param user_name
	 *            the user_name to set
	 */
	public void setUseName(String user_name) {
		this.user_name = user_name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber
	 *            the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	public ArrayList<Item> getItems() {
		return items;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Keeps track of the number of times the user has given a bad password to
	 * check for locking the user.
	 * 
	 * @return The number of times the user has given a bad password.
	 */
	public int hasBadPassword() {
		passwordCount++;
		return passwordCount;
	}

	/**
	 * @return The number of times a user has given a bad password at login.
	 */
	public int getPasswordCount() {
		return passwordCount;
	}

	/**
	 * @param i
	 *            The amount of times the user has given a bad password.
	 */
	public void setPasswordCount(int i) {
		passwordCount = i;
	}

	/**
	 * @return True if the account is locked; false if otherwise.
	 */
	public boolean isLocked() {
		return locked;
	}

	/**
	 * Locks the account.
	 * 
	 * @param b
	 *            True to lock the account; false to unlock it.
	 */
	public void lock(boolean b) {
		locked = b;
	}

	/**
	 * @return True if user is admin; false if user is not an admin.
	 */
	public boolean isAdmin() {
		return admin;
	}

	/**
	 * Sets a user's admin status.
	 * 
	 * @param b
	 *            True if user if to become an admin; false if the user is to be
	 *            demoted from admin.
	 */
	public void setIsAdmin(boolean b) {
		admin = b;
	}

	/**
	 * Adds an item the user created to their profile's private collection.
	 * 
	 * @param i
	 *            Item to add to user's private collection.
	 */
	public void addItem(Item i) {
		items.add(i);
	}

	/**
	 * Determines if the user has the given item.
	 * 
	 * @param i
	 *            The item to search for.
	 * @return True if the user has the item; false if otherwise.
	 */
	public boolean hasItem(Item i) {
		return items.contains(i);
	}

	/**
	 * @return All lost items created by this user.
	 */
	public ArrayList<Item> getLostItems() {
		ArrayList<Item> toReturn = new ArrayList<Item>();
		for (Item i : items) {
			if (i.isLost() == 0)
				toReturn.add(i);
			Log.w("item", i.toString());
		}
		return toReturn;
	}

	/**
	 * @return All found items created by this user.
	 */
	public ArrayList<Item> getFoundItems() {
		ArrayList<Item> toReturn = new ArrayList<Item>();
		for (Item i : items)
			if (i.isLost() == 1)
				toReturn.add(i);
		return toReturn;
	}

}
